package com.example.springboot.controller;

import com.example.springboot.constant.ResConstansts;
import com.example.springboot.entity.Post;
import com.example.springboot.entity.Res;
import com.example.springboot.entity.dto.PostDTO;
import com.example.springboot.entity.vo.PostVo;
import com.example.springboot.service.ESService;
import com.example.springboot.service.PostService;
import com.example.springboot.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/posts")
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private UserService userService;
    @Autowired
    private ESService esService;
    // 获取所有帖子
    //单条件查询
    @GetMapping
    public ResponseEntity<List<PostVo>> findPage(@RequestParam Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize ,
    @RequestParam(required = false) String searchContent, @RequestParam(required = false) Integer authorId,
    @RequestParam(required = false) Integer categoryId, @RequestParam(required = false) String tags,
@RequestParam(required = false) Integer articleType,@RequestParam(required = false) Integer homeOptions,
                                                 @RequestParam(required = false) Integer orderType) {
        Integer offset = (pageNum - 1) * pageSize;
        try{
            List<PostVo> posts = new ArrayList<>();
            if(searchContent!=null || articleType!=null){
                System.out.println(1);
                posts=esService.searchPost(searchContent,categoryId,articleType,orderType,offset,pageSize);
                esService.insertSearchSuggest(searchContent);
            }else if(authorId!=null){
                System.out.println(2);
            }else if(categoryId!=null){
                System.out.println(3);
                posts = postService.findAll(offset, pageSize,categoryId);
            }else if(tags!=null){
                System.out.println(4);
            }   else if(homeOptions!=null){
                System.out.println(5);
                switch (homeOptions){
                    case 0:
                        //TODO 先用查询所有占位
                        posts = postService.findAll(offset, pageSize);
                        break;
                }
            }
            return new ResponseEntity<>(posts, HttpStatus.OK);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    // 获取单个帖子
    @GetMapping("/{postId}")
    public ResponseEntity<Post> find(@PathVariable Integer postId) {
        Post post = postService.findOneById(postId);
        if(post == null){
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }else{
            return new ResponseEntity<>(post, HttpStatus.OK);
        }

    }

    // 创建新帖子
    @PostMapping
    public ResponseEntity<Post> create(@RequestBody PostDTO postDTO) {
        Integer authorId =userService.getAuthorId(postDTO.getTokens());
        Post post =new Post();
        BeanUtils.copyProperties(postDTO, post);
        post.setAuthorId(authorId);
        postService.insert(post);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }
    @GetMapping("/search_suggest")
    public Res<List<String>> searchSuggest(@RequestParam String searchContent){
        try{
            List<String> res = esService.generateSearchSuggest(searchContent);
            return new Res<List<String>>().code(200).data(res).msg("获取成功");
        }catch (Exception e){
            System.out.println(e);
            return ResConstansts.SERVICE_ERROR;
        }
    }
    @PostMapping("/{id}")
    public Res<Void> addViews(@PathVariable Integer id){
        try{
            postService.addViews(id);
            return ResConstansts.SUCCESS;
        }catch (Exception e){
            System.out.println(e);
            return ResConstansts.SERVICE_ERROR;
        }
    }
}
